#include "evdictCUI.h"
#include "evdictbase.h"
#include "evdictutil.h"
#include <conio.h>
#include <string.h>
#include <iostream>
using namespace std;

//int a = 0; // bien dia chi dung tam

// ham hien thi khung chuong trinh
void showLayoutScreen()
{
	system("cls");
	cout << "\n\t\t\t PHAM MEM TU DIEN ANH - VIET\n\n\n\n";
	cout << "________________________________________________________________________________";
	gotoxy(0, 21);
	cout << "________________________________________________________________________________";
}

// Ham nhap vao 1 tu
char* enterWord()
{
	char* word;
	word = new char[MAX_WORD_LENGTH + 1];
	gotoxy(1, 3);
	cout << "Nhap tu: ";
	fflush(stdin);
	gets(word);

	// kiem tra do dai tu vua nhap
	while ((strlen(word) > MAX_WORD_LENGTH) || (strlen(word) == 0))
	{	
		showLayoutScreen();
		gotoxy(2, 19);
		cout << "Do dai toi da cua 1 tu la " << MAX_WORD_LENGTH << " va toi thieu la 1 ki tu. Moi ban nhap lai!";

		gotoxy(1, 3);
		cout << "Nhap tu: ";
		fflush(stdin);
		gets(word);

		// xoa thong bao loi
		gotoxy(1, 19);
		cout << "\t\t\t\t\t\t\t\t\t\t";
	}

	return word;
}

// Ham hien thi 1 thong bao cho nguoi dung
void showMessage(char* message)
{
	gotoxy(5, 8);
	cout << message;
}

// Ham hien thi 1 thong bao cho nguoi dung
void showMessage(char* message, int x, int y)
{
	gotoxy(x, y);
	cout << message;
}

// Ham hoi nguoi dung co chac chan 1 thao tac nao khong
boolean showConfirmDialog(char* question)
{
	// xoa cac ki tu tren khung noi dung
	gotoxy(5, 8);
	cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
	char choice;
	gotoxy(5, 8);
	cout << question;
	gotoxy(79, 24);
	choice = _getch();
	switch (choice)
	{
	case 13: // 13 la ma cua phim Enter
		return TRUE;
	default:
		return FALSE;
	}
}

// Ham hien thi noi dung cua 1 tu
void showWordDefinition(WordDefinition word)
{
	gotoxy(5, 8);
	cout << word.word << "\n";
	cout << "\n\t- Tu loai: " << word.partOfSpeech;
	cout << "\n\n\t- Nghia tieng Viet: " << word.mean;
	cout << "\n\n\t- Vi du: " << word.example;
}

// Ham nhap vao noi dung cua 1 tu
WordDefinition enterWordDefinition(char* word)
{
	WordDefinition theWordDefinition;
	// xoa thong bao va noi dung dang co trong khung
	gotoxy(5, 8);
	cout << "\t\t\t\t\t\t\t\t\t\t";
	gotoxy(5, 10);
	cout << "\t\t\t\t\t\t\t\t\t\t";
	gotoxy(5, 12);
	cout << "\t\t\t\t\t\t\t\t\t\t";
	gotoxy(5, 14);
	cout << "\t\t\t\t\t\t\t\t\t\t";

	gotoxy(5, 8);
	cout << word << "\n";
	strcpy(theWordDefinition.word, word);

	cout << "\n\t- Tu loai: ";
	fflush(stdin);
	gets(theWordDefinition.partOfSpeech);

	// kiem tra do dai tu loai
	while ((strlen(theWordDefinition.partOfSpeech) > MAX_PART_OF_SPEECH_LENGTH) || (strlen(theWordDefinition.partOfSpeech) == 0))
	{	
		gotoxy(1, 19);
		cout << "Do dai toi da cua tu loai la " << MAX_PART_OF_SPEECH_LENGTH << " va toi thieu la 1 ki tu. Moi ban nhap lai!";
		
		// xoa bo cac ki tu phan vua nhap de nhap moi
		gotoxy(19, 10);
		int partOfSpeechLength = strlen(theWordDefinition.partOfSpeech);
		for (char i = 0; i <= partOfSpeechLength; i++)
		{
			cout << " ";
		}

		gotoxy(19, 10);
		fflush(stdin);
		gets(theWordDefinition.partOfSpeech);

		// xoa thong bao loi
		gotoxy(1, 19);
		cout << "\t\t\t\t\t\t\t\t\t\t";
	}
	
	gotoxy(0, 12);
	cout << "\t- Nghia tieng Viet: ";
	fflush(stdin);
	gets(theWordDefinition.mean);
	
	// kiem tra do dai nghia
	while ((strlen(theWordDefinition.mean) > MAX_MEAN_LENGTH) || (strlen(theWordDefinition.mean) == 0))
	{	
		gotoxy(1, 19);
		cout << "Do dai toi da cua nghia la " << MAX_MEAN_LENGTH << " va toi thieu la 1 ki tu. Moi ban nhap lai!";

		// xoa bo cac ki tu phan vua nhap de nhap moi
		gotoxy(28, 12);
		int meanLength = strlen(theWordDefinition.mean);
		for (char i = 0; i <= meanLength; i++)
		{
			cout << " ";
		}

		gotoxy(28, 12);
		fflush(stdin);
		gets(theWordDefinition.mean);

		// xoa thong bao loi
		gotoxy(1, 19);
		cout << "\t\t\t\t\t\t\t\t\t\t";
	}

	gotoxy(0, 14);
	cout << "\t- Vi du: ";
	fflush(stdin);
	gets(theWordDefinition.example);
	
	// kiem tra do dai vi du
	while ((strlen(theWordDefinition.example) > MAX_EXAMPLE_LENGTH) || (strlen(theWordDefinition.example) == 0))
	{	
		gotoxy(1, 19);
		cout << "Do dai toi da cua vi du la " << MAX_EXAMPLE_LENGTH << " va toi thieu la 1 ki tu. Moi ban nhap lai!";

		// xoa bo cac ki tu phan vua nhap de nhap moi
		gotoxy(17, 14);
		int exampleLength = strlen(theWordDefinition.example);
		for (char i = 0; i <= exampleLength; i++)
		{
			cout << " ";
		}

		gotoxy(17, 14);
		fflush(stdin);
		gets(theWordDefinition.example);
		
		// xoa thong bao loi
		gotoxy(1, 19);
		cout << "\t\t\t\t\t\t\t\t\t\t";
	}

	return theWordDefinition;
}

// Ham hien thi menu chuc nang khi bat dau chuong trinh, chi co Enter=Nhap tu, Esc=Thoat
void showContextMenu()
{
	char choice;
	gotoxy(2,23);
	cout << "Enter = Nhap tu\tEsc = Thoat";
	gotoxy(79,24);
	choice = _getch();
	switch (choice)
	{
		case 27: // 27 la ma cua phim Esc
			if (showConfirmDialog("Ban co chac chan muon thoat khong? (Enter = Co | Esc = Khong)"))
			{
				system("cls");
				exit(0);
			}
			break;
	}
}

// Ham hien thi menu chuc nang sau khi nhap 1 tu
void showContextMenu(int address, char* word, char* hashCode)
{
	char choice;
	gotoxy(2,23);

	if (address == NOT_FOUND)
	{
		cout << "Enter = Tiep tuc\tT = Them tu\tEsc = Thoat";
		gotoxy(79,24);
		choice = _getch();
		switch (choice)
		{
			case 27: // 27 la ma cua phim Esc
				if (showConfirmDialog("Ban co chac chan muon thoat khong? (Enter = Co | Esc = Khong)"))
				{
					system("cls");
					exit(0);
				}
				break;
			case 't':
				WordDefinition theWordDefinition;
				theWordDefinition = enterWordDefinition(word);
				if (isBackupListEmpty())
				{
					address = writeWordToFile(theWordDefinition, DICTIONARY_DATA_FILE);
					if (address == FAILED)
					{
						showMessage("Co loi khi ghi vao file Dictionary");
						break;
					}
					WordIndex theWordIndex;
					strcpy_s(theWordIndex.word, word);
					strcpy_s(theWordIndex.hashCode, hashCode);
					theWordIndex.address = address;
					if (writeWordToFile(theWordIndex, DICTIONARY_INDEX_FILE) == FAILED)
					{
						showMessage("Co loi khi ghi vao file Index");
						break;
					}
				} 
				else
				{
					address = getAvailableAddress();
					writeWordToFile(theWordDefinition,DICTIONARY_DATA_FILE,address);
				}
				addWordToHashTable(word, hashCode, address);
				showMessage("Them tu thanh cong! An phim bat ki de tiep tuc...", 5, 19);
				_getch();
				break;
		}
	}
	else
	{
		cout << "Enter = Tiep tuc\tS = Sua tu\tX = Xoa tu\tEsc = Thoat";
		gotoxy(79,24);
		choice = _getch();
		switch (choice)
		{
			case 27: // 27 la ma cua phim Esc
				if (showConfirmDialog("Ban co chac chan muon thoat khong? (Enter = Co | Esc = Khong)"))
				{
					system("cls");
					exit(0);
				}
				break;
			case 's':
				WordDefinition theWordDefinition;
				theWordDefinition = enterWordDefinition(word);
				writeWordToFile(theWordDefinition, DICTIONARY_DATA_FILE, address);
				showMessage("Sua tu thanh cong! An phim bat ki de tiep tuc...", 5, 19);
				_getch();
				break;
			case 'x':
				if (showConfirmDialog("Ban co chac chan xoa tu nay khong? (Enter = Co | Esc = Khong)"))
				{
					deleteWordFromHashTable(word, hashCode);
					addWordToBackUpList(address);
					//deleteWordFormIndex(word, DICTIONARY_INDEX_FILE);
					showMessage("Da xoa tu! An phim bat ki de tiep tuc...", 5, 19);
				}
				else
				{
					showMessage("An phim bat ki de tiep tuc...", 5, 19);
				}
				_getch();
				break;
		}
	}
}